package 栈.数组模拟栈.中缀计算器;

public class Calculator {
    public static void main(String[] args) {
        String s = "22+4*2-1"; // 9
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);
        int num1 = 0, num2 = 0, oper = 0, res = 0, index = 0;
        char ch = ' ';
        String plus = "";

        while (true) {
            ch = s.substring(index, index + 1).charAt(0);
            // 如果ch是数，则直接入栈
            if (numStack.isNum(ch)) {
                plus += ch;
                if (index == s.length() - 1) { // 临界条件，表达式末尾减1情况
                    numStack.push(Integer.parseInt(plus));
                }
                if (index != s.length() - 1) {
                    if (operStack.isOper(s.substring(index + 1, index + 2).charAt(0))) { // 如果后一位是运算符，则不拼接，直接将当前数压栈
                        numStack.push(Integer.parseInt(plus));
                        plus = "";
                    }
                }
            }
            index++;
            if (index >= s.length()) {
                break;
            }

            // 如果ch是符号
            if (operStack.isOper(ch)) {
                // 如果符号栈为空，则直接入栈
                if (operStack.isEmpty()) {
                    operStack.push(ch);
                    continue;
                }
                // 如果符号栈不为空，则判断符号优先级
                if (!operStack.isEmpty()) {
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) { // 栈顶运算符优先级高，先取出运算，勿忘最后将当前运算符压栈
                        oper = operStack.pop();
                        num2 = numStack.pop();
                        num1 = numStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }
                    if (operStack.priority(ch) > operStack.priority(operStack.peek())) {
                        operStack.push(ch);
                    }
                }
            }
        }
        while (true) {
            if (operStack.isEmpty()) { // 数栈栈底为结果
                break;
            }
            num2 = numStack.pop();
            num1 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }
        int finalRes = numStack.pop();
        System.out.println("表达式s=" + s + "计算结果为：" + finalRes);

    }

}

class ArrayStack {
    int top = -1; // 栈顶默认为-1
    private int[] stack = null;
    private int maxSize; // 栈的最大容量

    public ArrayStack(int size) {
        maxSize = size;
        stack = new int[size];
    }

    public void push(int value) {
        if (top == maxSize - 1) {
            throw new RuntimeException("栈满，元素不能入栈");
        }
        stack[++top] = value;
    }

    public int pop() {
        if (top == -1) {
            throw new RuntimeException("栈空，没有元素可以出栈");
        }
        int value = stack[top--];
        return value;
    }

    public void list() { // 遍历栈的时候不能移动top，因为出栈方法才可去移动top
        int temp = top;
        for (int i = temp; i >= 0; i--) {
            System.out.println(stack[temp--]);
        }
    }

    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num1 - num2;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num1 / num2;
                break;
        }
        return res;
    }

    public boolean isNum(char ch) {
        return ch != '+' && ch != '-' && ch != '*' && ch != '/';
    }


    public boolean isOper(char ch) {
        return ch == '+' || ch == '-' || ch == '*' || ch == '/';
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public int priority(int ch) {
        if (ch == '*' || ch == '/') {
            return 1;
        }
        if (ch == '+' || ch == '-') {
            return 0;
        }
        return -1;
    }

    public int peek() {
        return stack[top];
    }
}